<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="充值订单流水号" prop="rechargeNumber">
            <el-input v-model="queryParams.rechargeNumber" placeholder="请输入充值订单流水号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="用户手机号" prop="userMobile">
            <el-input v-model="queryParams.userMobile" placeholder="请输入用户手机号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['member:MemberRechargeOrder:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['member:MemberRechargeOrder:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['member:MemberRechargeOrder:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['member:MemberRechargeOrder:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="MemberRechargeOrderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="充值订单id" align="center" prop="rechargeOrderId" v-if="true" />
        <el-table-column label="充值订单流水号" align="center" prop="rechargeNumber" />
        <el-table-column label="用户id" align="center" prop="userId" />
        <el-table-column label="用户名" align="center" prop="userName" />
        <el-table-column label="用户手机号" align="center" prop="userMobile" />
        <el-table-column label="用户类型，0-普通用户，1-Vip用户" align="center" prop="userType" />
        <el-table-column label="充值金额" align="center" prop="rechargeMoney" />
        <el-table-column label="充值设置id" align="center" prop="rechargeConfigId" />
        <el-table-column label="会员设置id" align="center" prop="infoConfigId" />
        <el-table-column label="会员设置名称" align="center" prop="infoConfigName" />
        <el-table-column label="支付状态，1：未支付，2：已支付" align="center" prop="payStatus" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default={row}>
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(row)" v-hasPermi="['member:MemberRechargeOrder:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(row)" v-hasPermi="['member:MemberRechargeOrder:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改充值订单对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="MemberRechargeOrderFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="充值订单流水号" prop="rechargeNumber">
          <el-input v-model="form.rechargeNumber" placeholder="请输入充值订单流水号" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="用户手机号" prop="userMobile">
          <el-input v-model="form.userMobile" placeholder="请输入用户手机号" />
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeMoney">
          <el-input v-model="form.rechargeMoney" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="充值设置id" prop="rechargeConfigId">
          <el-input v-model="form.rechargeConfigId" placeholder="请输入充值设置id" />
        </el-form-item>
        <el-form-item label="会员设置id" prop="infoConfigId">
          <el-input v-model="form.infoConfigId" placeholder="请输入会员设置id" />
        </el-form-item>
        <el-form-item label="会员设置名称" prop="infoConfigName">
          <el-input v-model="form.infoConfigName" placeholder="请输入会员设置名称" />
        </el-form-item>
        <el-form-item label="支付请求报文" prop="payReqContents">
            <el-input v-model="form.payReqContents" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付请求时间" prop="payReqTime">
          <el-date-picker clearable
            v-model="form.payReqTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择支付请求时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付订单响应" prop="rawData">
            <el-input v-model="form.rawData" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付响应编码" prop="payRespCode">
          <el-input v-model="form.payRespCode" placeholder="请输入支付响应编码" />
        </el-form-item>
        <el-form-item label="支付响应信息" prop="payRespMsg">
          <el-input v-model="form.payRespMsg" placeholder="请输入支付响应信息" />
        </el-form-item>
        <el-form-item label="支付响应报文" prop="payRespContents">
            <el-input v-model="form.payRespContents" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付响应时间" prop="payRespTime">
          <el-date-picker clearable
            v-model="form.payRespTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择支付响应时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MemberRechargeOrder" lang="ts">
import { listMemberRechargeOrder, getMemberRechargeOrder, delMemberRechargeOrder, addMemberRechargeOrder, updateMemberRechargeOrder } from '@/api/member/MemberRechargeOrder';
import { MemberRechargeOrderVO, MemberRechargeOrderQuery, MemberRechargeOrderForm } from '@/api/member/MemberRechargeOrder/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const MemberRechargeOrderList = ref<MemberRechargeOrderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const MemberRechargeOrderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: MemberRechargeOrderForm = {
  rechargeOrderId: undefined,
  rechargeNumber: undefined,
  userId: undefined,
  userName: undefined,
  userMobile: undefined,
  userType: undefined,
  rechargeMoney: undefined,
  rechargeConfigId: undefined,
  infoConfigId: undefined,
  infoConfigName: undefined,
  payStatus: undefined,
  payType: undefined,
  payReqContents: undefined,
  payReqTime: undefined,
  rawData: undefined,
  payRespCode: undefined,
  payRespMsg: undefined,
  payRespContents: undefined,
  payRespTime: undefined,
}
const data = reactive<PageData<MemberRechargeOrderForm, MemberRechargeOrderQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    rechargeNumber: undefined,
    userName: undefined,
    userMobile: undefined,
    userType: undefined,
    params: {
    }
  },
  rules: {
    rechargeOrderId: [
      { required: true, message: "充值订单id不能为空", trigger: "blur" }
    ],
    rechargeNumber: [
      { required: true, message: "充值订单流水号不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" }
    ],
    userName: [
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    userMobile: [
      { required: true, message: "用户手机号不能为空", trigger: "blur" }
    ],
    userType: [
      { required: true, message: "用户类型，0-普通用户，1-Vip用户不能为空", trigger: "change" }
    ],
    rechargeMoney: [
      { required: true, message: "充值金额不能为空", trigger: "blur" }
    ],
    rechargeConfigId: [
      { required: true, message: "充值设置id不能为空", trigger: "blur" }
    ],
    infoConfigId: [
      { required: true, message: "会员设置id不能为空", trigger: "blur" }
    ],
    infoConfigName: [
      { required: true, message: "会员设置名称不能为空", trigger: "blur" }
    ],
    payStatus: [
      { required: true, message: "支付状态，1：未支付，2：已支付不能为空", trigger: "change" }
    ],
    payType: [
      { required: true, message: "支付类型，1：微信，2：支付宝不能为空", trigger: "change" }
    ],
    payReqContents: [
      { required: true, message: "支付请求报文不能为空", trigger: "blur" }
    ],
    payReqTime: [
      { required: true, message: "支付请求时间不能为空", trigger: "blur" }
    ],
    rawData: [
      { required: true, message: "支付订单响应不能为空", trigger: "blur" }
    ],
    payRespCode: [
      { required: true, message: "支付响应编码不能为空", trigger: "blur" }
    ],
    payRespMsg: [
      { required: true, message: "支付响应信息不能为空", trigger: "blur" }
    ],
    payRespContents: [
      { required: true, message: "支付响应报文不能为空", trigger: "blur" }
    ],
    payRespTime: [
      { required: true, message: "支付响应时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询充值订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMemberRechargeOrder(queryParams.value);
  MemberRechargeOrderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  MemberRechargeOrderFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: MemberRechargeOrderVO[]) => {
  ids.value = selection.map(item => item.rechargeOrderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加充值订单";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: MemberRechargeOrderVO) => {
  reset();
  const _rechargeOrderId = row?.rechargeOrderId || ids.value[0]
  const res = await getMemberRechargeOrder(_rechargeOrderId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改充值订单";
}

/** 提交按钮 */
const submitForm = () => {
  MemberRechargeOrderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.rechargeOrderId) {
        await updateMemberRechargeOrder(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addMemberRechargeOrder(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: MemberRechargeOrderVO) => {
  const _rechargeOrderIds = row?.rechargeOrderId || ids.value;
  await proxy?.$modal.confirm('是否确认删除充值订单编号为"' + _rechargeOrderIds + '"的数据项？').finally(() => loading.value = false);
  await delMemberRechargeOrder(_rechargeOrderIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('member/MemberRechargeOrder/export', {
    ...queryParams.value
  }, `MemberRechargeOrder_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
